﻿using System;
using System.Windows.Forms;
using Business;
using Business.Dicionary;
using Business.Entity.Contabil.Pessoa.SubClass.Empresa;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Sistema.Terminal;
using Business.Entity.Sistema.Terminal.ECF;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Sistema
{
    public partial class FormConfTerminal : FormDefault, IForm
    {
        public FormConfTerminal()
        {
            InitializeComponent();
            VerificaSituacaoTabs();
            InitFocusControl = descricaoTextEdit;
            EmpresaRepository.Session = ConfTerminalRepository.GetSession();
            fabricanteEcfDictionaryBindingSource.DataSource = new FabricanteEcfDictionary();
            empresaBindingSource.DataSource = EmpresaRepository.GetList();
        }

        private void VerificaSituacaoTabs()
        {
            var current = (ConfTerminal)confTerminalBindingSource.Current;
            if (ckbAtivo.Checked &&
                (ckbLocadoraAtivo.Checked || ckbMerceariaAtivo.Checked || ckbRestauranteAtivo.Checked) &&
                current != null)
            {
                tabConfEcf.Enabled = true;
                tabConfTef.Enabled = true;

                if (current.ConfiguracaoEcf == null)
                {
                    current.ConfiguracaoEcf = new ConfiguracaoEcf();
                }
                if (current.ConfiguracaoTef == null)
                {
                    current.ConfiguracaoTef = new ConfiguracaoTef();
                }
                
            }
            else
            {
                tabConfEcf.Enabled = false;
                tabConfTef.Enabled = false;
                

            }
        }

        private void merceariaAtivoCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            VerificaSituacaoTabs();
        }
        
        private void cmdConfigurarPdv_Click(object sender, EventArgs e)
        {
            var conf = (ConfTerminal)confTerminalBindingSource.Current;
            if (conf.ConfiguracaoEcf == null)
                conf.ConfiguracaoEcf = new ConfiguracaoEcf();
            if (conf.ConfiguracaoTef == null)
                conf.ConfiguracaoTef = new ConfiguracaoTef();
            tabConfEcf.Enabled = true;
            tabConfTef.Enabled = true;
        }

        public object Novo()
        {
            var conf = new ConfTerminal();
            confTerminalBindingSource.DataSource = conf;
            ShowDialog();
            if (conf.Id == 0)
            {
                return null;
            }
            return confTerminalBindingSource.Current;
        }

        public void Alterar(object entity)
        {

            confTerminalBindingSource.DataSource = entity;
            VerificaSituacaoTabs();
            ShowDialog();
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                confTerminalBindingSource.EndEdit();
                var t = (ConfTerminal)confTerminalBindingSource.Current;
                if (t.Id == 0)
                {
                    var chave = DateTime.Now.ToString("ddmmyyyyHHmmss");
                    chave += t.Empresa.Pessoa.CpfCnpj;
                    t.Chave = new Criptografia.CriptHash().GetHash(chave);
                }
                ConfTerminalRepository.Save(t);
                Hide();
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            ConfTerminalRepository.Delete((ConfTerminal) entity);
            return true;
        }


        public object CurrentObject
        {
            get
            {
                confTerminalBindingSource.EndEdit();
                return confTerminalBindingSource.DataSource;
            }
            set
            {
                confTerminalBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            var conf = (ConfTerminal) CurrentObject;
            var result = DataValidation.ValidateEntity(conf);
            foreach (var error in DataValidation.ValidateEntity(conf.ConfiguracaoEcf).ValidationErrors)
            {
                result.ValidationErrors.Add(error);
            }
            foreach (var error in DataValidation.ValidateEntity(conf.ConfiguracaoTef).ValidationErrors)
            {
                result.ValidationErrors.Add(error);
            }
            return result;
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}
